package com.betterjr.modules.risk.loan.entity;

import java.math.BigDecimal;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import com.betterjr.common.annotation.MetaData;
import com.betterjr.common.entity.BetterjrEntity;
import com.betterjr.common.selectkey.SerialGenerator;
import com.betterjr.common.utils.MathExtend;
import com.betterjr.modules.risk.loan.data.CalculateChargeFactory;
import com.betterjr.modules.risk.loan.data.LoanTransactionRelationConstantCollections;

@Access(AccessType.FIELD)
@Entity
@Table(name = "t_scf_loan_service")
public class LoanProjectCharge implements BetterjrEntity {

    /** 
     * @Fields serialVersionUID : 
     */
    private static final long serialVersionUID = 4810779837123261219L;
    /**
     * 流水号
     */
    @Id
    @Column(name = "ID", columnDefinition = "INTEGER")
    @MetaData(value = "流水号", comments = "流水号")
    private Long id;

    /**
     * 来源贷款项目
     */
    @Column(name = "L_LOAN_ID", columnDefinition = "INTEGER")
    @MetaData(value = "来源贷款项目", comments = "来源贷款项目")
    private Long loanId;

    /**
     * 供应商名称
     */
    @Column(name = "C_CUSTNAME", columnDefinition = "VARCHAR")
    @MetaData(value = "供应商名称", comments = "供应商名称")
    private String custName;

    /**
     * 供应商编号
     */
    @Column(name = "L_CUSTNO", columnDefinition = "INTEGER")
    @MetaData(value = "供应商编号", comments = "供应商编号")
    private Long custNo;

    /**
     * 供应商企业代码
     */
    @Column(name = "C_CUSTCODE", columnDefinition = "VARCHAR")
    @MetaData(value = "供应商企业代码", comments = "供应商企业代码")
    private String custCode;

    /**
     * 核心企业名称
     */
    @Column(name = "C_CORENAME", columnDefinition = "VARCHAR")
    @MetaData(value = "核心企业名称", comments = "核心企业名称")
    private String coreName;

    /**
     * 核心企业编号
     */
    @Column(name = "L_CORENO", columnDefinition = "INTEGER")
    @MetaData(value = "核心企业编号", comments = "核心企业编号")
    private Long coreNo;

    /**
     * 资金提供方名称
     */
    @Column(name = "C_FACTORYNAME", columnDefinition = "VARCHAR")
    @MetaData(value = "资金提供方名称", comments = "资金提供方名称")
    private String factoryName;

    /**
     * 资金提供方编号
     */
    @Column(name = "L_FACTORYNO", columnDefinition = "INTEGER")
    @MetaData(value = "资金提供方编号", comments = "资金提供方编号")
    private Long factoryNo;

    /**
     * 放款日期
     */
    @Column(name = "C_LOAN_DATE", columnDefinition = "VARCHAR")
    @MetaData(value = "放款日期", comments = "放款日期")
    private String loanDate;

    /**
     * 融资期限
     */
    @Column(name = "N_FINANCINGMATURITY_DATE", columnDefinition = "INTEGER")
    @MetaData(value = "融资期限", comments = "融资期限")
    private Long financingMaturityDate;

    /**
     * 放款金额
     */
    @Column(name = "F_LOAN_BALANCE", columnDefinition = "DOUBLE")
    @MetaData(value = "放款金额", comments = "放款金额")
    private BigDecimal loanBalance;

    /**
     * 平台服务费率
     */
    @Column(name = "F_SERVICE_RATE", columnDefinition = "DOUBLE")
    @MetaData(value = "平台服务费率", comments = "平台服务费率")
    private BigDecimal serviceRate;

    /**
     * 服务费
     */
    @Column(name = "F_SERVICE_CHARGE", columnDefinition = "DOUBLE")
    @MetaData(value = "服务费", comments = "服务费")
    private BigDecimal serviceCharge;

    /**
     * 收取服务费时间
     */
    @Column(name = "C_GATHERING_DATE", columnDefinition = "VARCHAR")
    @MetaData(value = "收取服务费时间", comments = "收取服务费时间")
    private String gatheringDate;

    /**
     * 收取服务费方式 1现金转账
     */
    @Column(name = "C_GATHERING_TYPE", columnDefinition = "VARCHAR")
    @MetaData(value = "收取服务费方式 1现金转账", comments = "收取服务费方式 1现金转账")
    private String gatheringType;

    /**
     * 服务费状态 1：待收取服务费   2 已收取  3 作废
     */
    @Column(name = "C_BUSINSTATUS", columnDefinition = "VARCHAR")
    @MetaData(value = "服务费状态 1：待收取服务费   2 已收取  3 作废", comments = "服务费状态 1：待收取服务费   2 已收取  3 作废")
    private String businStatus;

    /**
     * 描述说明
     */
    @Column(name = "C_DESCRIPTION", columnDefinition = "VARCHAR")
    @MetaData(value = "描述说明", comments = "描述说明")
    private String description;

    /**
     * 核心企业组织结构代码
     */
    @Column(name = "C_COREOPERORG", columnDefinition = "VARCHAR")
    @MetaData(value = "核心企业组织结构代码", comments = "核心企业组织结构代码")
    private String coreOperorg;

    /**
     * 结算周期 0 按季度结算  1 按月结算
     */
    @Column(name = "C_SETTLEACCOUNTS_TYPE", columnDefinition = "VARCHAR")
    @MetaData(value = "结算周期 0 按季度结算  1 按月结算", comments = "结算周期 0 按季度结算  1 按月结算")
    private String settleAccountsType;

    /**
     * 分润比例
     */
    @Column(name = "F_SHARE_SCALE", columnDefinition = "DOUBLE")
    @MetaData(value = "分润比例", comments = "分润比例")
    private BigDecimal shareScale;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getLoanId() {
        return loanId;
    }

    public void setLoanId(Long loanId) {
        this.loanId = loanId;
    }

    public String getCustName() {
        return custName;
    }

    public void setCustName(String custName) {
        this.custName = custName;
    }

    public Long getCustNo() {
        return custNo;
    }

    public void setCustNo(Long custNo) {
        this.custNo = custNo;
    }

    public String getCustCode() {
        return custCode;
    }

    public void setCustCode(String custCode) {
        this.custCode = custCode;
    }

    public String getCoreName() {
        return coreName;
    }

    public void setCoreName(String coreName) {
        this.coreName = coreName;
    }

    public Long getCoreNo() {
        return coreNo;
    }

    public void setCoreNo(Long coreNo) {
        this.coreNo = coreNo;
    }

    public String getFactoryName() {
        return factoryName;
    }

    public void setFactoryName(String factoryName) {
        this.factoryName = factoryName;
    }

    public Long getFactoryNo() {
        return factoryNo;
    }

    public void setFactoryNo(Long factoryNo) {
        this.factoryNo = factoryNo;
    }

    public String getLoanDate() {
        return loanDate;
    }

    public void setLoanDate(String loanDate) {
        this.loanDate = loanDate;
    }

    public Long getFinancingMaturityDate() {
        return financingMaturityDate;
    }

    public void setFinancingMaturityDate(Long financingMaturityDate) {
        this.financingMaturityDate = financingMaturityDate;
    }

    public BigDecimal getLoanBalance() {
        return loanBalance;
    }

    public void setLoanBalance(BigDecimal loanBalance) {
        this.loanBalance = loanBalance;
    }

    public BigDecimal getServiceRate() {
        return serviceRate;
    }

    public void setServiceRate(BigDecimal serviceRate) {
        this.serviceRate = serviceRate;
    }

    public BigDecimal getServiceCharge() {
        return serviceCharge;
    }

    public void setServiceCharge(BigDecimal serviceCharge) {
        this.serviceCharge = serviceCharge;
    }

    public String getGatheringDate() {
        return gatheringDate;
    }

    public void setGatheringDate(String gatheringDate) {
        this.gatheringDate = gatheringDate;
    }

    public String getGatheringType() {
        return gatheringType;
    }

    public void setGatheringType(String gatheringType) {
        this.gatheringType = gatheringType;
    }

    public String getBusinStatus() {
        return businStatus;
    }

    public void setBusinStatus(String businStatus) {
        this.businStatus = businStatus;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getCoreOperorg() {
        return coreOperorg;
    }

    public void setCoreOperorg(String coreOperorg) {
        this.coreOperorg = coreOperorg;
    }

    public String getSettleAccountsType() {
        return settleAccountsType;
    }

    public void setSettleAccountsType(String settleAccountsType) {
        this.settleAccountsType = settleAccountsType;
    }

    public BigDecimal getShareScale() {
        return shareScale;
    }

    public void setShareScale(BigDecimal shareScale) {
        this.shareScale = shareScale;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((businStatus == null) ? 0 : businStatus.hashCode());
        result = prime * result + ((coreName == null) ? 0 : coreName.hashCode());
        result = prime * result + ((coreNo == null) ? 0 : coreNo.hashCode());
        result = prime * result + ((custCode == null) ? 0 : custCode.hashCode());
        result = prime * result + ((custName == null) ? 0 : custName.hashCode());
        result = prime * result + ((custNo == null) ? 0 : custNo.hashCode());
        result = prime * result + ((description == null) ? 0 : description.hashCode());
        result = prime * result + ((factoryName == null) ? 0 : factoryName.hashCode());
        result = prime * result + ((factoryNo == null) ? 0 : factoryNo.hashCode());
        result = prime * result + ((financingMaturityDate == null) ? 0 : financingMaturityDate.hashCode());
        result = prime * result + ((gatheringDate == null) ? 0 : gatheringDate.hashCode());
        result = prime * result + ((gatheringType == null) ? 0 : gatheringType.hashCode());
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((loanBalance == null) ? 0 : loanBalance.hashCode());
        result = prime * result + ((loanDate == null) ? 0 : loanDate.hashCode());
        result = prime * result + ((loanId == null) ? 0 : loanId.hashCode());
        result = prime * result + ((serviceCharge == null) ? 0 : serviceCharge.hashCode());
        result = prime * result + ((serviceRate == null) ? 0 : serviceRate.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof LoanProjectCharge)) {
            return false;
        }
        LoanProjectCharge other = (LoanProjectCharge) obj;
        if (businStatus == null) {
            if (other.businStatus != null) {
                return false;
            }
        } else if (!businStatus.equals(other.businStatus)) {
            return false;
        }
        if (coreName == null) {
            if (other.coreName != null) {
                return false;
            }
        } else if (!coreName.equals(other.coreName)) {
            return false;
        }
        if (coreNo == null) {
            if (other.coreNo != null) {
                return false;
            }
        } else if (!coreNo.equals(other.coreNo)) {
            return false;
        }
        if (custCode == null) {
            if (other.custCode != null) {
                return false;
            }
        } else if (!custCode.equals(other.custCode)) {
            return false;
        }
        if (custName == null) {
            if (other.custName != null) {
                return false;
            }
        } else if (!custName.equals(other.custName)) {
            return false;
        }
        if (custNo == null) {
            if (other.custNo != null) {
                return false;
            }
        } else if (!custNo.equals(other.custNo)) {
            return false;
        }
        if (description == null) {
            if (other.description != null) {
                return false;
            }
        } else if (!description.equals(other.description)) {
            return false;
        }
        if (factoryName == null) {
            if (other.factoryName != null) {
                return false;
            }
        } else if (!factoryName.equals(other.factoryName)) {
            return false;
        }
        if (factoryNo == null) {
            if (other.factoryNo != null) {
                return false;
            }
        } else if (!factoryNo.equals(other.factoryNo)) {
            return false;
        }
        if (financingMaturityDate == null) {
            if (other.financingMaturityDate != null) {
                return false;
            }
        } else if (!financingMaturityDate.equals(other.financingMaturityDate)) {
            return false;
        }
        if (gatheringDate == null) {
            if (other.gatheringDate != null) {
                return false;
            }
        } else if (!gatheringDate.equals(other.gatheringDate)) {
            return false;
        }
        if (gatheringType == null) {
            if (other.gatheringType != null) {
                return false;
            }
        } else if (!gatheringType.equals(other.gatheringType)) {
            return false;
        }
        if (id == null) {
            if (other.id != null) {
                return false;
            }
        } else if (!id.equals(other.id)) {
            return false;
        }
        if (loanBalance == null) {
            if (other.loanBalance != null) {
                return false;
            }
        } else if (!loanBalance.equals(other.loanBalance)) {
            return false;
        }
        if (loanDate == null) {
            if (other.loanDate != null) {
                return false;
            }
        } else if (!loanDate.equals(other.loanDate)) {
            return false;
        }
        if (loanId == null) {
            if (other.loanId != null) {
                return false;
            }
        } else if (!loanId.equals(other.loanId)) {
            return false;
        }
        if (serviceCharge == null) {
            if (other.serviceCharge != null) {
                return false;
            }
        } else if (!serviceCharge.equals(other.serviceCharge)) {
            return false;
        }
        if (serviceRate == null) {
            if (other.serviceRate != null) {
                return false;
            }
        } else if (!serviceRate.equals(other.serviceRate)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "LoanProjectCharge [id=" + id + ", loanId=" + loanId + ", custName=" + custName + ", custNo=" + custNo
                + ", custCode=" + custCode + ", coreName=" + coreName + ", coreNo=" + coreNo + ", factoryName="
                + factoryName + ", factoryNo=" + factoryNo + ", loanDate=" + loanDate + ", financingMaturityDate="
                + financingMaturityDate + ", loanBalance=" + loanBalance + ", serviceRate=" + serviceRate
                + ", serviceCharge=" + serviceCharge + ", gatheringDate=" + gatheringDate + ", gatheringType="
                + gatheringType + ", businStatus=" + businStatus + ", description=" + description + ", coreOperorg="
                + coreOperorg + ", settleAccountsType=" + settleAccountsType + ", shareScale=" + shareScale + "]";
    }

    public void saveAddInitValue() {

        this.setId(SerialGenerator.getLongValue("LoanProjectCharge.id"));

        this.setBusinStatus("1");

    }

    public void calculateCharge() {
        // 贷款金额*费率/360*融资期限
        if (this.loanBalance != null && this.getServiceRate() != null && this.financingMaturityDate != null
                && this.financingMaturityDate != 0l) {

            this.setServiceCharge(CalculateChargeFactory.invoke(this));

        }

    }

}
